//
//  HLLiveUserListView.m
//  YueYue
//
//  Created by hulu001 on 2024/3/12.
//

#import "HLLiveUserListView.h"
#import "HLZegoLiveKit.h"
#import "HLLiveAudienceRequest.h"
#import "HLLiveRankRequest.h"
#import "HLLiveUserRankView.h"

@interface HLLiveUserListView ()<UITableViewDelegate,UITableViewDataSource, YTKRequestDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) QMUILabel *titleLabel;
@property (nonatomic, strong) NSMutableArray<HLLiveUserModel *> *users;
@property (nonatomic, strong) HLLiveAudienceRequest *request;
@property (nonatomic, strong) HLLiveUserRankView *myRankView;

@end

@implementation HLLiveUserListView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorWhite;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 12;
        self.users = [NSMutableArray array];
        [self setupView];
        
    }
    return self;
}

- (void)setupView {
    
    self.request = [[HLLiveAudienceRequest alloc] initWithType:HLLiveAudienceAll room:HLZegoLiveKit.sharedInstance.female.liveRoomCode];
    _request.delegate = self;

    self.titleLabel = [[QMUILabel alloc] init];
    _titleLabel.text = @"在线观众";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = HLTextColor1;
    _titleLabel.font = UIFontMediumMake(16);
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(16);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero];
    _tableView.pagingEnabled = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = UIColorClear;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[HLLiveUserListCell class] forCellReuseIdentifier:[HLLiveUserListCell identifyCell]];
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_titleLabel.mas_bottom).offset(16);
        make.bottom.mas_equalTo(-68);
    }];
    
    self.myRankView = [[HLLiveUserRankView alloc] init];
    [self addSubview:_myRankView];
    [_myRankView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
    }];
    
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [header setTitle:@"" forState:MJRefreshStateIdle];
    self.tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    self.tableView.mj_footer = footer;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.users.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HLLiveUserListCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLLiveUserListCell identifyCell]];
    if (self.users.count <= indexPath.row){
        return cell;
    }
    cell.model = self.users[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

- (void)headerRefresh
{
    self.request.page = 1;
    [self.request start];
    [self myRankRequest];
}

- (void)footerRefresh
{
    self.request.page += 1;
    [self.request start];
}

- (void)requestFinished:(__kindof HLLiveAudienceRequest *)request
{
    [self endAnimateRefresh];
    if (request.page == 1){
        [self.users removeAllObjects];
    }
    [self.users addObjectsFromArray:[request userList]];
    
    [self.tableView reloadData];
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    [QMUITips showWithText:[request.error localizedDescription]];
    [self endAnimateRefresh];
    
}

- (void)noMordDataWithFooterRefresh
{
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}

- (void)endAnimateRefresh
{
    if ([self.tableView.mj_header isRefreshing]){
        [self.tableView.mj_header endRefreshing];
    }
    
    if ([self.tableView.mj_footer isRefreshing]){
        [self.tableView.mj_footer endRefreshing];
    }
}

- (void)myRankRequest
{
    [[[HLLiveRankRequest alloc] initWithRoom:HLZegoLiveKit.sharedInstance.female.liveRoomCode] startWithCompletionBlockWithSuccess:^(__kindof HLLiveRankRequest * _Nonnull request) {
        if (request.isSuccess) {
            self.myRankView.model = [request data];
        }
    } failure:nil];
}

+ (void)show
{
    
    CGFloat height = 490;

    HLLiveUserListView *contentView = [[HLLiveUserListView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    
    
    QMUIModalPresentationViewController *modalViewController = [[QMUIModalPresentationViewController alloc] init];
    modalViewController.contentViewMargins = UIEdgeInsetsMake(SCREEN_HEIGHT-height, 0, 0, 0);
    modalViewController.contentView = contentView;
    modalViewController.dimmingView.backgroundColor = [UIColorBlack colorWithAlphaComponent:0.3];
    [modalViewController showWithAnimated:YES completion:^(BOOL finished) {
        [contentView.tableView.mj_header beginRefreshing];
    }];
    
//    [contentView.closeButton setQmui_tapBlock:^(__kindof UIControl *sender) {
//        [modalViewController hideWithAnimated:NO completion:nil];
//    }];
//
//    contentView.closeCallback = ^(BOOL isClose) {
//        [modalViewController hideWithAnimated:NO completion:nil];
//    };
}



@end
